﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Microsoft.InformationSecurity.CISF.Common.Utilities;
using System.DirectoryServices.AccountManagement;

namespace Microsoft.InformationSecurity.CISF.SecurityPortal.UI.WebTemplate.MasterUI
{
    public partial class PortalHome : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Getting User Full Name from Session
            string userFullName = (string)Session["UserFullName"];

            //return the user name from session if already exists
            if (userFullName == null)
            {
                Session["UserFullName"] = (string)Utility.GetUserFullName(Page.User.Identity.Name.ToString());
                userFullName = (string)Session["UserFullName"];
            }

            //set the full user name or user alias to the welcome message
            this.UserNameLabel.Text = userFullName;

            //set the Privacy statement in the footer
            this.lnkPrivacyStatement.Text = ConfigurationManager.AppSettings["PrivacyLinkText"].ToString();
            this.lnkPrivacyStatement.NavigateUrl = "/Privacy.aspx";
            //loading top nav
            loadTopNavigation();
        }

        protected void loadTopNavigation()
        {
            SiteMapNode node = SiteMap.CurrentNode;
            if ((node != null) && (SiteMap.RootNode != null))
            {
                int offset = 0;
                while ((node.ParentNode != null) && (!node.ParentNode.Equals(SiteMap.RootNode)))
                {
                    offset--;
                    node = node.ParentNode;
                }
                //if (offset != 0)
                //{
                //    PortalChildMenuDS.StartingNodeOffset = offset;
                //}
            }
        }

        protected void PrimaryNavItemDataBound(object sender, MenuEventArgs e)
        {
            SiteMapNode node = SiteMap.CurrentNode;
            if (SiteMap.RootNode != null)
            {
                while ((node != null) && (node.ParentNode != null) && (!node.ParentNode.Equals(SiteMap.RootNode)))
                {
                    node = node.ParentNode;
                }
                if ((node != null) && (node.ParentNode != null) && node.ParentNode.Equals(SiteMap.RootNode) && node.Url.Equals(e.Item.NavigateUrl))
                {
                    e.Item.Selected = true;
                }
            }
        }

    }
}
